package com.xxx.controller;

import com.xxx.publisher.DemoEventPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试spring event
 */
@Slf4j
@RestController
@RequestMapping("/event")
public class TestController implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @GetMapping("/publish")
    public String publish(String msg){
        // 获取发布者对象
        DemoEventPublisher bean = applicationContext.getBean(DemoEventPublisher.class);
        if(StringUtils.isEmpty(msg)) {
            msg = "测试消息";
        }
        log.info("发布者发送消息：" + msg);
        bean.sendMsg(msg);
        return "发布者发送消息成功";
    }
}
